const connection = require("../app/database")

class FileService{
  async uploadAvatar(fieldname,mimetype,size,userId){
    console.log(fieldname,mimetype,size,userId);
    const statement = `INSERT INTO avatar (filename,mimetype,size,user_id) VALUES (?,?,?,?)`;

    let [result] = await connection.promise().execute(statement,[fieldname,mimetype,size,userId])
    return result
  }

  async getAvatarByUserId(userId){
    const statement = `SELECT * FROM avatar WHERE user_id = ?`;
    let [result] = await connection.promise().execute(statement,[userId])
    return result[0]
  }

  async createPicture(filename,mimetype,size,userId,momentId){
    const statement = `INSERT INTO file (filename,mimetype,size,user_id,moment_id) VALUES (?,?,?,?,?)`;

    let [result] = await connection.promise().execute(statement,[filename,mimetype,size,userId,momentId])
    return result
  }

  async getFileByFilename(filename){
    const statement = `SELECT * FROM file WHERE filename = ?`;
    let [result] = await connection.promise().execute(statement,[filename])
    return result[0]
  }
}

module.exports = new FileService()